package C037;

import com.sun.source.tree.Tree;

import java.util.Stack;

/**
 * ClassName: C04
 * Package: C037
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/23 16:14
 * @Version 1.0
 * @Since 1.0
 */
public class C04 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(2);
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(3);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(3);
        treeNode.left = treeNode1;
        treeNode.right = treeNode2;
        treeNode2.right = treeNode3;
        treeNode3.right = treeNode4;
        System.out.println(isBalanced2(treeNode));
    }

    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    static boolean flag = true;

    public static boolean isBalanced2(TreeNode root) {
        flag = true;
        process(root);
        return flag;
    }

    private static int process(TreeNode root) {
        if (!flag || root == null) {
            return 0;
        }

        int l = process(root.left);
        int r = process(root.right);
        if (Math.abs(r - l) > 1) {
            flag = false;
            return 0;
        }

        return 1 + Math.max(l, r);
    }


}
